/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import doa.TwittererDAO;
import java.io.Serializable;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import models.Twitterer;
import utils.JSFSessionHandler;

/**
 *
 * @author Misja
 */
public class TwittererOverviewBean implements Serializable {

    private List<Twitterer> twitterers;

    public TwittererOverviewBean() {
        this.twitterers = TwittererDAO.getTwitterers();
    }

    public List<Twitterer> getTwitterers() {
        return twitterers;
    }

    public void setTwitterers(List<Twitterer> twitterers) {
        this.twitterers = twitterers;
    }
    
    public void addFavorite(Twitterer favorite) {
        Twitterer temp = (Twitterer)JSFSessionHandler.getObjectFromSession("currentUser");
        temp.getFavorites().add(favorite);
        if (TwittererDAO.updateTwitterer(temp)) {
            JSFSessionHandler.putObjectInSession("currentUser", temp);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Twitterer updated", null));
        } else {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Twitterer not updated", null));
        }
    }
    
    public String readMessages(Twitterer twitterer) {
        JSFSessionHandler.putObjectInSession("selectedUser", twitterer);
        //FacesContext.getCurrentInstance().getExternalContext().getFlash().put("twitterer", twitterer);
        return "/MessageOverview.xhtml?faces-redirect=true";
    }
    
    public String toHome(){
        return "/Home.xhtml?faces-redirect=true";
    }
}
